<template>
  <div class="addnewgood-content">
    <div class="newgoodimgbox jiushibox">
      <div>
        <h1 class="tit">录入商品图片</h1>
        <!-- <img
          src="@/assets/img/img5_cd.png"
          class="upimged"
          v-for="(item,index) in items"
          :key="index"
        > -->
        <div class="imgbox">
          <img :src="fan?fan:up" class="upimg" @click="upimg">
        </div>
      </div>
      <van-cell-group>
        <van-field v-model="name" label="商品名" placeholder="请输入商品名"/>
      </van-cell-group>
      <van-cell-group>
        <van-field v-model="mark" label="商品备注" placeholder="请输入商品备注"/>
      </van-cell-group>
      <van-cell-group>
        <van-field v-model="price" label="商品单价" placeholder="请输入价格" type="number"/>
      </van-cell-group>
      <van-cell-group>
        <van-field v-model="num" label="商品库存" placeholder="请选择商品剩余数量"/>
        <div class="tplist" @click="Suk_showtplist"></div>
        <van-popup v-model="showtp" position="bottom">
          <van-picker
            :columns="columns"
            @cancel="onCancelnum"
            @confirm="onConfirmnum"
            show-toolbar
          />
        </van-popup>
      </van-cell-group>
      <van-cell-group>
        <van-field v-model="tp" label="商品类型" placeholder="请选择商品类型"/>
        <div class="tplist" @click="Suk_showlist"></div>
        <van-popup v-model="show" position="bottom">
          <van-picker :columns="list" @cancel="onCanceltp" @confirm="onConfirmtp" show-toolbar/>
        </van-popup>
      </van-cell-group>
      <van-cell-group>
        <van-field v-model="num2" label="是否食堂" placeholder="请选择是否需要现场核销"/>
        <div class="tplist" @click="Suk_showlist2"></div>
        <van-popup v-model="show2" position="bottom">
          <van-picker :columns="list2" @cancel="onCancel2" @confirm="onConfirm2" show-toolbar/>
        </van-popup>
      </van-cell-group>

      <van-cell-group>
        <van-field v-model="creattime" @focus="showcreate = true" placeholder="请选择生效时间" label="生效时间"/>
        <van-field v-model="stoptime" @focus="showend = true" placeholder="请选择失效时间" label="失效时间"/>

      </van-cell-group>
      <van-popup v-model="showcreate" position="bottom">
        <van-datetime-picker
          v-model="currentDate"
          type="time"
          :min-date="minDate"
          :formatter="formatter"
          @confirm="checkcratetime"
          @cancel="showcreate = false"
        />
      </van-popup>

      <van-popup v-model="showend" position="bottom">
        <van-datetime-picker
          v-model="currentDate"
          type="time"
          :min-date="minDate"
          :formatter="formatter"
          @confirm="checkendtime"
          @cancel="showend = false"
        />
      </van-popup>
    </div>
    <div class="addbtn" @click="Suk_tosavegood">保存新增</div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      fan: null,
      up: require('@/assets/img/img_zopian_cwsj.png'),
      mark: "", //商品备注

      minDate: new Date(),
      currentDate: new Date(),
      show: false,
      show2: false,
      list: [],
      list2: ["是", "否"],
      showtp: false, //这是展示分类选择
      columns: ["库存无限", "库存不足", "库存为空"],
      name: "",
      price: "",
      num: "",
      num2: "",
      tp: "",
      tp2: "",
      items: [],
      tpid: [],
      img: [],
      classid: null,

      showend: false,
      showcreate: false,
      creattime: "",
      stoptime: ""
    };
  },
  beforeCreate: function () {
    //创建前
  },
  created: function () {
    this.Suk_gettplist();
    //创建后
  },
  beforeMount: function () {
    //挂载前
  },
  mounted: function () {
    //挂载后
  },
  beforeUpdate: function () {
    //更新前
  },
  update: function () {
    //更新后
  },
  methods: {
    // 下面的是我们的开始时间
    checkcratetime(e) {
      this.creattime = e;
      this.showcreate = false;
    },
    checkendtime(e) {
      this.stoptime = e;
      this.showend = false;
    },
    formatter(type, value) {
      if (type === 'hour') {
        return `${value}时`
      } else if (type === 'minute') {
        return `${value}分`
      } else if (type === 'second') {
        return `${value}秒`
      }
    },
    Suk_tosavegood() {
      let that = this;
      let goodnums = null
      that.$toast.loading({
        message: "加载中..."
      });
      if (that.num == '库存为空') {
        goodnums = 1
      } else if (that.num == '库存不足') {
        goodnums = 2
      } else if (that.num == '库存无限') {
        goodnums = 3
      }
      var Sukdata = {
        remark: that.mark,
        image: that.fan,
        diningId: localStorage.getItem('myshopid'),
        productName: that.name,
        price: that.price,
        productNums: goodnums,
        classId: that.classid,
        isroom: that.num2 == "是" ? 1 : 0,
        showcreate:that.creattime,
        showend:that.stoptime
      };
      console.log(Sukdata);
      that.axios
        .post(that.baseUrl + "/product/setProduct", that.Qs.stringify(Sukdata), {
          headers: {
            "Content-Type": "application/x-www-form-urlencoded"
          }
        })
        .then(function (res) {
          if (res.request.status === 200) {
            if (res.data.meta.code == 200) {
              console.log(res.data);
              that.$toast.success({
                message: "添加成功！",
              });
              setTimeout(() => {
                that.$router.go(-1)
              }, 1000);
            } else if (res.data.meta.code == 501) {
            }
          }
        });
    },
    // 上传商品图片
    upimg() {
      let that = this;
      dsBridge.call("uploadImg", 10);
      dsBridge.register("addImgCallback", function (data) {
        that.fan = data;
        // let str = JSON.parse(data)
        // that.$toast.loading({
        //   message: "加载中..."
        // });
        // alert("12132")
        var Sukdata = {
          zurl: data
        };
        console.log(Sukdata);
        that.axios
          .post(
            that.baseUrl + "/store/storeZheng",
            that.Qs.stringify(Sukdata),
            {
              headers: {
                "Content-Type": "application/x-www-form-urlencoded"
              }
            }
          )
          .then(function (res) {
            if (res.request.status === 200) {
              if (res.data.meta.code == 200) {
                // alert(res.data)
                // alert(that.zimg)
                // that.$toast.clear();
              } else if (res.data.meta.code == 501) {
              }
            }
          });
      });
    },
    // 获取分类列表
    Suk_gettplist() {
      let that = this;
      that.$toast.loading({
        message: "加载中..."
      });
      var Sukdata = {
        dinId: localStorage.getItem("myshopid")
      };
      // console.log(Sukdata);
      that.axios
        .post(that.baseUrl + "/classs/queryClass", that.Qs.stringify(Sukdata), {
          headers: {
            "Content-Type": "application/x-www-form-urlencoded"
          }
        })
        .then(function (res) {
          if (res.request.status === 200) {
            if (res.data.meta.code == 200) {
              //   console.log(res.data.data)
              that.tpid = res.data.data
              let arr = [];
              for (let i = 0; i < res.data.data.length; i++) {
                // console.log(res.data.data)
                arr.push(res.data.data[i].className);
              }
              that.list = arr;
              //   console.log(arr)
              that.$toast.clear();
            } else if (res.data.meta.code == 501) {
            }
          }
        });
    },

    Suk_showlist() {
      let that = this;
      that.show = true;
    },
    Suk_showlist2() {
      let that = this;
      that.show2 = true;
    },
    onCancelnum() {
      let that = this;
      that.showtp = false;
    },
    onConfirm2(value, index) {

      let that = this;
      that.num2 = value;
      that.show2 = false;
    },
    onCanceltp() {
      let that = this;
      that.show = false;
    },
    onCancel2() {
      let that = this;
      that.show2 = false;
    },
    onConfirmnum(value, index) {
      //这里是我们的选择确定
      let that = this;
      that.num = value;
      that.showtp = false;
    },
    onConfirmtp(value, index) {
      //这里是我们的选择确定
      let that = this;
      //   console.log(that.tpid[index].classId)
      that.classid = that.tpid[index].classId
      that.tp = value;
      that.show = false;
    },
    Suk_showtplist() {
      let that = this;
      that.showtp = true;
    },
    onRead(file) {
      console.log(file);
    }
  }
};
</script>
<style lang="scss" scoped>
.addnewgood-content {
  font-size: 0.3rem;
}

.imgbox {
  position: relative;
  margin-top: 0.5rem;
  margin-left: 0.3rem;
  margin-bottom: 0.2rem;
}

.upimg {
  width: 3.5rem;
}

.upimged {
  width: 3rem;
  margin-top: 0.2rem;
  margin-bottom: 0.2rem;
}

.tit {
  font-weight: 500;
}

.showupimg {
  width: 3.5rem;
  height: 3rem;
  position: absolute;
  left: 0;
  opacity: 0;
}

.jiushibox {
  width: 90%;
  margin: 0 auto;
}

.addbtn {
  width: 2.75rem;
  height: 1rem;
  background: #fea600;
  color: #fff;
  text-align: center;
  line-height: 1rem;
  border-radius: 0.2rem;
  float: right;
  margin-right: 5%;
  margin-top: 0.3rem;
}

.opabox {
  opacity: 0;
  width: 80%;
  height: 1.2rem;
  right: 0;
  position: absolute;
  z-index: 999;
  top: 0;
}

.tplist {
  opacity: 0;
  width: 80%;
  height: 1.2rem;
  right: 0;
  position: absolute;
  z-index: 999;
  top: 0;
}

input {
  background: #f4f4f4;
}
</style>

